home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / snpd1292.zip / STRREV.C < prev    next >
C/C++ Source or Header  |  1992-12-26  |  644b  |  38 lines

  1. /*
  2. **  STRREV.C - reverse a string in place
  3. **
  4. **  public domain by Bob Stout
  5. */
  6.  
  7. #include <string.h>
  8.  
  9. char *strrev(char *str)
  10. {
  11.       char *p1, *p2;
  12.  
  13.       if (! str || ! *str)
  14.             return str;
  15.       for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
  16.       {
  17.             *p1 ^= *p2;
  18.             *p2 ^= *p1;
  19.             *p1 ^= *p2;
  20.       }
  21.       return str;
  22. }
  23.  
  24. #ifdef TEST
  25.  
  26. #include <stdio.h>
  27.  
  28. int main(int argc, char *argv[])
  29. {
  30.       while (--argc)
  31.       {
  32.             printf("\"%s\" backwards is ", *++argv);
  33.             printf("\"%s\"\n", strrev(*argv));
  34.       }
  35. }
  36.  
  37. #endif /* TEST */
  38.